﻿#region Copyrights Notice
//©2009 Microsoft Corporation.  All rights reserved.
//This portal design is based on the design and some code from DropThings.com which is also licensed under an MS-PL license.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.InformationSecurity.CISF.Portal.Business.PortalFramework;
using Microsoft.InformationSecurity.CISF.Portal.Business;

using FACTORY = Microsoft.InformationSecurity.RiskManagement.ISRMWeb.Common;

namespace Microsoft.InformationSecurity.RiskManagement.ISRMWeb.UserControls
{
    public partial class ContainerUserControl : System.Web.UI.UserControl, IWebletContainer
    {
        public WebletContainerBase ac = new WebletContainerBase();

        public event Action<RealWeblet> OnControlDelete;

        //getting user name from context
        string _UserName = HttpContext.Current.User.Identity.Name.Split('\\')[1].ToString();

        public bool ShowSettings
        {
            get
            {
                object val = ViewState[this.ClientID + "ShowDisplaySettings"] ?? false;
                return (bool)val;
            }
            set { ViewState[this.ClientID + "ShowDisplaySettings"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ContainerHeader1.Super = this as IWebletActions;
            ContainerHeader1.Title.Text = ac.RealWebletControl.Caption;
            ShowAndHide();
            LinkButton b = ContainerHeader1.CloseLinkButton;
            b.OnClientClick = "if (confirm('Are you sure you want to remove the widget from your home page?')) { return deleteWidget('" + ac.m_ctrlRealWebletControl.WidgetInstanceId + "'); } else { return false;}";
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            String vPath = ac.GetControlRelativeUrl();
            int nControlId = ac.GetControlID2();
            var currentControl = FACTORY.WebletControlFactory2.CreateWeblet(this, nControlId, vPath);
            IWeblet weblet = currentControl as IWeblet;
            MainDisplayArea.Controls.Add(currentControl);
            this.ac.m_ctrlWebletControl = weblet;
            weblet.OnControlInitilize(this);
        }

        private void ShowAndHide()
        {
            MainDisplayArea.Visible = ac.RealWebletControl.IsExpanded;
            ContainerHeader1.ExpandLinkButton.Visible = !ac.RealWebletControl.IsExpanded;
            ContainerHeader1.CollapseLinkButton.Visible = ac.RealWebletControl.IsExpanded;
        }

        protected void EditButton_Click(object sender, EventArgs e)
        {
            UpdateSetting();

        }
        public void DoMinimize()
        {
            (this as IWebletContainer).Minimize();
        }
        public void DoMaximize()
        {
            (this as IWebletContainer).Maximize();
        }
        public void DoClose()
        {
            this.ac.m_ctrlWebletControl.Close();
            (this as IWebletContainer).Close();
        }
        protected void SaveSettings_Click(object sender, EventArgs e)
        {
            UpdateSetting();
        }

        protected void CancelSettings_Click(object sender, EventArgs e)
        {
            UpdateSetting();
        }

        public void UpdateSetting()
        {
            (this as IWebletContainer).DisplaySettings(!this.ShowSettings);
            WidgetBodyUpdatePanel.Update();
        }
        void IWebletActions.UpdateSettings()
        {
            UpdateSetting();
        }

        protected void RevertSetting()
        {
            IWebletContainer c = this;
            c.DisplaySettings(!this.ShowSettings);
            WidgetBodyUpdatePanel.Update();

        }

        /* Begin - Implement IWebletContainer */
        int IWebletContainer.ControlID
        {
            get
            {
                return this.ac.RealWebletControl.WidgetInstanceId;
            }
        }
        void IWebletActions.MaximizeToFullScreen()
        {
            //Empty Implementation
        }

        void IWebletContainer.Save(string state)
        {
            //Todo -  Update database...
            var PortalFacade = new PortalFacade(_UserName);

            if (!PortalFacade.UpdateWidgetState(ac.RealWebletControl.WidgetId, state)) { } //Success
            else { }//Failure.
        }
        string IWebletContainer.Restore()
        {
            return ac.RealWebletControl.State;
        }
        void IWebletActions.Maximize()
        {
            //Todo -  Update database...
            var PortalFacade = new PortalFacade(_UserName);

            if (!PortalFacade.MaximizeWidget(ac.RealWebletControl.WidgetId))
            {
                ac.RealWebletControl.IsExpanded = true;
            } //Success
            else { }//Failure.

            ShowAndHide();

            WidgetBodyUpdatePanel.Update();
            WidgetHeaderUpdatePanel.Update();
        }
        void IWebletActions.Minimize()
        {
            //Todo -  Update database...
            var PortalFacade = new PortalFacade(_UserName);

            if (!PortalFacade.MinimizeWidget(ac.RealWebletControl.WidgetId))
            {
                ac.RealWebletControl.IsExpanded = false;
            } //Success
            else { }//Failure.

            this.ShowAndHide();

            WidgetBodyUpdatePanel.Update();
            WidgetHeaderUpdatePanel.Update();
        }
        void IWebletActions.Close()
        {
            //Update database.
            var PortalFacade = new PortalFacade(_UserName);

            if (!PortalFacade.DeleteWidget(ac.RealWebletControl.WidgetId)) { } //Success
            else { }//Failure.

            //Update UI.
            OnControlDelete(ac.RealWebletControl);
        }

        void IWebletActions.DisplaySettings(bool bShow)
        {
            this.ShowSettings = bShow;
            ac.m_ctrlWebletControl.DisplaySettings(bShow);//.DisplaySettings(true);

            if (bShow)
            {
                (this as IWebletContainer).Maximize();
            }

            this.WidgetHeaderUpdatePanel.Update();
        }

        public bool FirstTime
        {
            get { return ac.m_bLoadFirstTime; }
            set { ac.m_bLoadFirstTime = value; }
        }
    }
}